package com.xjtu.platform.common;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;


import com.xjtu.platform.common.page.QueryAction;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;



public class Utils {


    public static <T> Map<String,Object> getPage(PageParam pageParam , QueryAction<T> queryAction){
        //执行映射器方法之前立即执行分页参数（页码和每页记录数）的设置
        PageHelper.startPage(pageParam);

        List<T> list = queryAction.execute();

        //执行映射器方法之后，立即执行PageInfo对象的创建
        PageInfo<T> pageInfo = new PageInfo<T>(list);

        Map<String,Object> page = new HashMap<>();
        page.put("current", pageInfo.getPageNum());//当前页
        page.put("pageSize",pageInfo.getPageSize());//每页最大记录数
        page.put("total",pageInfo.getTotal());//总记录数
        page.put("pages",pageInfo.getPages());//总页数
        page.put("size",pageInfo.getSize());//当前页实际记录数
        page.put("list",pageInfo.getList());//当前页的数据记录
        page.put("first",1);
        page.put("pre",pageInfo.getPrePage());
        page.put("next",pageInfo.getNextPage());
        page.put("last",pageInfo.getPages());

        return page;

    }


    @Getter
    private static final Set<String> filter = new HashSet<>();



    static {
        filter.add("case_id");
        filter.add("pageSize");
        filter.add("pageNum");
        filter.add("sheet_type_id");
        filter.add("sheet_id");
    }

    @Getter
    private static final Map<String, Integer> sheet_type_map = new HashMap<>();
    static {
        sheet_type_map.put("potential_donator_info_sheet", 1);
        sheet_type_map.put("coordinator_family_before_info_sheet", 2);
        sheet_type_map.put("coordinator_family_after_info_sheet", 3);
        sheet_type_map.put("coordinator_supplement_sheet", 4);
        sheet_type_map.put("evaluation_info", 5);
        sheet_type_map.put("evaluation_pic_info", 6);
        sheet_type_map.put("evaluation_drug_sheet", 7);
        sheet_type_map.put("obtain_opreation_info", 8);
        sheet_type_map.put("organ_donate_case_record", 9);
        sheet_type_map.put("donate_record_sheet", 10);
        sheet_type_map.put("death_record_evulation", 11);
        sheet_type_map.put("acceptor_info", 12);
        sheet_type_map.put("ethical_approval_sheet", 13);
        sheet_type_map.put("evulation_supplement_info_sheet", 14);

    }

    @Getter
    private static final Map<Integer ,String> sheet_name_map = new HashMap<>();
    static {
        sheet_name_map.put(1, "potential_donator_info_sheet");
        sheet_name_map.put(2, "coordinator_family_before_info_sheet");
        sheet_name_map.put(3, "coordinator_family_after_info_sheet");
        sheet_name_map.put(4, "coordinator_supplement_sheet");
        sheet_name_map.put(5, "evaluation_info");
        sheet_name_map.put(6, "evaluation_pic_info");
        sheet_name_map.put(7, "evaluation_drug_sheet");
        sheet_name_map.put(8, "obtain_operation_info");
        sheet_name_map.put(9, "organ_donate_case_record");
        sheet_name_map.put(10, "donate_record_sheet");
        sheet_name_map.put(11, "death_record_evulation");
        sheet_name_map.put(12, "acceptor_info");
        sheet_name_map.put(13, "ethical_approval_sheet");
        sheet_name_map.put(14, "evulation_supplement_info_sheet");

    }

    @Getter
    private static final Map<String ,String> sheet_primary_map = new HashMap<>();
    static {
        sheet_primary_map.put("potential_donator_info_sheet", "potential_donator_info_sheet_id");
        sheet_primary_map.put("coordinator_family_before_info_sheet", "coordinator_family_before_info_sheet_id");
        sheet_primary_map.put("coordinator_family_after_info_sheet", "coordinator_family_after_info_sheet_id");
        sheet_primary_map.put("coordinator_supplement_sheet", "coordinator_supplement_sheet_id");
        sheet_primary_map.put("evaluation_info", "evaluation_info_id");
        sheet_primary_map.put("evaluation_pic_info", "evaluation_pic_info_id");
        sheet_primary_map.put("evaluation_drug_sheet", "evaluation_drug_sheet_id");
        sheet_primary_map.put("obtain_operation_info", "obtain_operation_info_id");
        sheet_primary_map.put("organ_donate_case_record", "organ_donate_case_record_id");
        sheet_primary_map.put("donate_record_sheet", "donate_record_sheet_id");
        sheet_primary_map.put("death_record_evulation", "death_record_evulatio_id");
        sheet_primary_map.put("acceptor_info", "acceptor_info_id");
        sheet_primary_map.put("ethical_approval_sheet", "ethical_approval_sheet_id");
        sheet_primary_map.put("evulation_supplement_info_sheet", "evulation_supplement_info_sheet_id");
    }

    @Getter
    private static final Map<Integer, String> event_type = new HashMap<>();
    static {
        event_type.put(1, "coordinator_accept");
        event_type.put(2, "obtain_doctor_accept");
        event_type.put(3, "evaluator_admin_accept");
        event_type.put(4, "ethics_committee_audit");
        event_type.put(5, "super_admin_end_archive");
        event_type.put(6, "super_admin_choose_coordinator");
        event_type.put(7, "super_admin_choose_evaluator");
        event_type.put(8, "drug_admin_accept");
        event_type.put(9, "experimenter_accept");
        event_type.put(10, "evaluator_upload_ethics");
    }




    /**
     * 将一个Java对象转换为json串，并向浏览器（客户端）输出
     * @param resp 响应对象
     * @param obj 需要转化为json串的java对象
     * @throws IOException
     */

    public static void outJson(HttpServletResponse resp, Object obj) throws IOException {
        //json格式的媒体标准：application/json
        resp.setContentType("application/json;charset=UTF-8");//设置字符编码
        PrintWriter out = resp.getWriter();//获取向客户端发送字符信息流对象
        // 将list集合对象转化为json格式字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(obj);
        out.print(jsonString);

        out.flush();
        out.close();
    }


}
